const app = getApp()
const db = wx.cloud.database();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: "",
    user: "",
    windowHeight: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取用户屏幕高度
    wx.getSystemInfo({
      success: (res) => {
        let windowHeight = res.windowHeight

        this.setData({
          windowHeight: windowHeight
        })
      }
    })
  },

  //获取输入内容
  input: function (data) {
    this.setData({
      content: data.detail.value
    })
  },

  //搜索按钮
  search: function () {
    
    let c = this.data.content;
    let type=app.globalData.type;
    console.log(c)
    const that=this;
    if(type==1){
    setTimeout(function () {
    //根据输入内容筛选数据库中检测记录
    db.collection('user')
      .where(_.or([{
          name: db.RegExp({ //使用正则查询，实现对搜索的模糊查询
            regexp: c,
            options: 'i', //大小写不区分
          }),
        },
        {
          idcardnum: db.RegExp({
            regexp: c,
            options: 'i',
          }),
        },
        {
          phonenum: db.RegExp({
            regexp: c,
            options: 'i',
          }),
        }
      ])).get()
      .then(res => {
        console.log('查询成功', res)
        that.setData({
        user: res.data
        })
      })
      .catch(res => {
        console.log('查询失败', res)
      })},80)
    }
    else if(type==2){
        setTimeout(function () {
        //根据输入内容筛选数据库中检测记录
        db.collection('user2')
          .where(_.or([{
              name: db.RegExp({ //使用正则查询，实现对搜索的模糊查询
                regexp: c,
                options: 'i', //大小写不区分
              }),
            },
            {
              idcardnum: db.RegExp({
                regexp: c,
                options: 'i',
              }),
            },
            {
              phonenum: db.RegExp({
                regexp: c,
                options: 'i',
              }),
            }
          ])).get()
          .then(res => {
            console.log('查询成功', res)
            that.setData({
            user: res.data
            })
          })
          .catch(res => {
            console.log('查询失败', res)
          })},80)
        }
        else if(type==3){
            setTimeout(function () {
            //根据输入内容筛选数据库中检测记录
            db.collection('user3')
              .where(_.or([{
                  name: db.RegExp({ //使用正则查询，实现对搜索的模糊查询
                    regexp: c,
                    options: 'i', //大小写不区分
                  }),
                },
                {
                  idcardnum: db.RegExp({
                    regexp: c,
                    options: 'i',
                  }),
                },
                {
                  phonenum: db.RegExp({
                    regexp: c,
                    options: 'i',
                  }),
                }
              ])).get()
              .then(res => {
                console.log('查询成功', res)
                that.setData({
                user: res.data
                })
              })
              .catch(res => {
                console.log('查询失败', res)
              })},80)
            }
    
  },

  onclick: function (e) {
    console.log(e.currentTarget.dataset.text);
       var fileID=e.currentTarget.dataset.text;
       if(fileID!=null&&fileID.length>0){
        wx.cloud.downloadFile({
            fileID:fileID,
            success:res =>{
                wx.openDocument({
                    filePath: res.tempFilePath,
                    showMenu: true,
                    fileType: 'docx',
                    success: function (ress) {
                      console.log('打开文档成功', ress)
                    },
                    fail:function (ress) {
                        console.log('打开文档失败', ress)
                    }
                  })
                wx.showToast({
                  title: '打开成功',
                  icon:"success",
                  duration:2000
                })
            },
            fail:err => {
                console.log("打开失败",err)
            }
        })
    }else{
        wx.showToast({
          title: '未找到文件',
          icon:"none"
        })
    }
  },
  onclick2: function (e) {
    console.log(e);
    app.globalData.userid = e.currentTarget.dataset.text;
  }


})